from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session
from typing import List

from app.database.database import get_db
from app.database.repository.account_repository import AccountRepository
from app.exception.code_exception import CodeException
from app.schemas.account import AccountDTO, AccountRecharge
from app.core.pagination import Pageable, Page
from app.schemas.rr import RR, SuccessView
from app.services.account_service import AccountService
router = APIRouter(prefix="/account", tags=["account"])
import logging
logger = logging.getLogger(__name__)

@router.get("/list", response_model=RR[Page[AccountDTO]])
async def get_accounts(
    pageable: Pageable = Depends(),
    db: Session = Depends(get_db)
):
    page = AccountRepository(db).find_page(pageable=pageable)
    
    # 记录日志
    for account in page.items:
        logger.info(f"Account User ID: {account.user_id}, User: {account.user}")
    
    return RR.success(page)

@router.get("/{account_id}", response_model=RR[AccountDTO])
async def get_account(account_id: int, db: Session = Depends(get_db)):
    account = AccountRepository(db).find_by_id(account_id)
    if not account:
        raise CodeException.resource_not_found("账户不存在")
    return RR.success(account)

@router.post("/{account_id}/recharge", response_model=RR[AccountDTO], status_code=status.HTTP_200_OK)
async def recharge(account_id: int, recharge_data: AccountRecharge, db: Session = Depends(get_db)):
    account_service = AccountService(db)
    updated_account = account_service.recharge(account_id, recharge_data)
    return RR.success(updated_account)